package com.model.common.base.fragment;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.viewbinding.ViewBinding;

import com.model.common.R;
import com.model.common.utils.always.StringUtils;

import org.greenrobot.eventbus.EventBus;

/**
 * Create by Carson on 2021/12/22.
 * BaseAppFragment
 */
public abstract class BaseFragment<VB extends ViewBinding> extends Fragment
        implements View.OnClickListener {

    private static final int NON_CODE = -1;
    protected Context mContext;
    protected VB binding;
    protected Dialog loadingDialog;

    //加载布局
    protected abstract VB getViewBinding(LayoutInflater inflater, ViewGroup viewGroup);

    protected abstract void initViewsAndEvents();

    /*是否绑定eventbus事件* */
    protected boolean isBindEventBusHere() {
        return false;
    }

    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        mContext = context;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (isBindEventBusHere()) {
            EventBus.getDefault().register(this);
        }
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        binding = getViewBinding(inflater, container);
        return binding.getRoot();
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        //屏幕适配
        DisplayMetrics displayMetrics = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
        initViewsAndEvents();
        addListener();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        hideLoading();
        if (isBindEventBusHere()) {
            EventBus.getDefault().unregister(this);
        }
        binding = null;
    }

    public synchronized void hideLoading() {
        Log.e("loadingDialog", ">>>>>>  hideLoading");
        if (loadingDialog != null) {
            loadingDialog.dismiss();
        }
    }

    public synchronized void showLoading(String message) {
        Log.e("loadingDialog", ">>>>>>  showLoading");
        if (loadingDialog == null) {
            loadingDialog = new Dialog(mContext, R.style.NoTitleDialogStyle);
            loadingDialog.setContentView(R.layout.loading_view);
            loadingDialog.setCanceledOnTouchOutside(false);
        }
        loadingDialog.show();
        TextView loadingMessage = loadingDialog.findViewById(R.id.tv_loading_message);
        if (message == null || StringUtils.isEmpty(message)) {
            loadingMessage.setVisibility(View.GONE);
        } else {
            loadingMessage.setVisibility(View.VISIBLE);
            loadingMessage.setText(message);
        }
    }

    /**
     * 给 View 控件添加点击事件监听器
     */
    protected void addListener() {
    }

    protected void onWidgetClick(View view) {
    }

    private long lastTime = 0L;
    private View currentView = null;

    @Override
    public void onClick(View view) {
        long nowTime = System.currentTimeMillis();
        if (nowTime - lastTime > 1000L || view.getId() == currentView.getId()) {
            lastTime = nowTime;
            currentView = view;
            onWidgetClick(view);
        }
    }

    /**
     * startActivity
     *
     * @param clazz target Activity
     */
    protected void go(Class<? extends Activity> clazz) {
        _goActivity(clazz, null, NON_CODE, false);
    }

    /**
     * startActivity with bundle
     *
     * @param clazz target Activity
     */
    protected void go(Class<? extends Activity> clazz, Bundle bundle) {
        _goActivity(clazz, bundle, NON_CODE, false);
    }

    protected void goForResult(Class<? extends Activity> clazz, int requestCode) {
        _goActivity(clazz, null, requestCode, false);
    }

    protected void goForResult(Class<? extends Activity> clazz, Bundle bundle, int requestCode) {
        _goActivity(clazz, bundle, requestCode, false);
    }

    /**
     * Activity 跳转
     *
     * @param clazz  目标activity
     * @param bundle 传递参数
     * @param finish 是否结束当前activity
     */
    private void _goActivity(Class<? extends Activity> clazz, Bundle bundle, int requestCode, boolean finish) {
        if (null == clazz) {
            throw new IllegalArgumentException("you must pass a target activity where to go.");
        }
        Intent intent = new Intent(getActivity(), clazz);
        intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        if (null != bundle) {
            intent.putExtras(bundle);
        }
        if (requestCode > NON_CODE) {
            startActivityForResult(intent, requestCode);
        } else {
            startActivity(intent);
        }
        if (finish) {
            getActivity().finish();
        }
    }
}